package com.zy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.dao.ItemMapper;
import com.zy.dao.ItemStockMapper;
import com.zy.dataobject.Item;
import com.zy.dataobject.ItemExample;
import com.zy.dataobject.ItemStock;
import com.zy.error.BusinessException;
import com.zy.error.EmBussinessError;
import com.zy.service.IItemService;
import com.zy.service.model.ItemModel;
import com.zy.validator.ValidateResult;
import com.zy.validator.ValidatorUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhaoyang on 2019/6/20.
 */
@Service("itemService")
public class ItemServiceImpl implements IItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemStockMapper itemStockMapper;
    @Override
    public ItemModel getItemById(Integer id) throws BusinessException {
        if (id == null) {
            throw new BusinessException(EmBussinessError.PARAMETER_VALIDATE_ERROR,"商品id为空");
        }
        Item item = itemMapper.selectByPrimaryKey(id);
        if (item == null) {
            throw new BusinessException(EmBussinessError.ITEM_NOT_EXIT_ERROR);
        }
        ItemStock itemStock = itemStockMapper.selectByItemId(id);
        ItemModel itemModel = convertToItemModel(item, itemStock);

        return itemModel;
    }

    @Override
    public List<ItemModel> listItem(ItemExample itemExample) {
        PageHelper.startPage(1,20);
        List<Item> items = itemMapper.selectByExample(itemExample);
        List<ItemModel> itemModelList = items.stream().map(item -> {
            ItemStock itemStock = itemStockMapper.selectByItemId(item.getId());
            ItemModel itemModel = convertToItemModel(item, itemStock);
            return itemModel;
        }).collect(Collectors.toList());

        return itemModelList;
    }

    @Override
    @Transactional
    public Integer addItem(ItemModel itemModel) throws BusinessException {
        if (itemModel == null) {
            throw new BusinessException(EmBussinessError.PARAMETER_VALIDATE_ERROR,"商品数据有误");
        }
        ValidateResult validateResult = ValidatorUtil.validate(itemModel);
        if (validateResult.hasError()) {
            throw new BusinessException(EmBussinessError.PARAMETER_VALIDATE_ERROR,validateResult.getErrMsg());
        }
        Item item = convertToItem(itemModel);
        int i = itemMapper.insertSelective(item);
        ItemStock itemStock = new ItemStock();
        itemStock.setStock(itemModel.getStock());
        itemStock.setItemId(i);
        itemStockMapper.insertSelective(itemStock);
        return i;
    }

    @Override
    public void updateItem(ItemModel itemModel) throws BusinessException {
        if (itemModel == null) {
            throw new BusinessException(EmBussinessError.PARAMETER_VALIDATE_ERROR,"商品数据有误");
        }
        ValidateResult validateResult = ValidatorUtil.validate(itemModel);
        if (validateResult.hasError()) {
            throw new BusinessException(EmBussinessError.PARAMETER_VALIDATE_ERROR,validateResult.getErrMsg());
        }
        Item item = convertToItem(itemModel);
        itemMapper.updateByPrimaryKeySelective(item);
//        ItemStock itemStock = new ItemStock();
//        itemStock.setItemId(itemModel.getId());
//        itemStock.setStock(itemModel.getStock());
        ItemStock itemStock = itemStockMapper.selectByItemId(item.getId());
        itemStock.setStock(itemModel.getStock());
        itemStockMapper.updateByPrimaryKey(itemStock);
    }

    @Override
    public boolean decreaseStock(Integer itemId, Integer amount) throws BusinessException {
        if (itemId <= 0 || amount <= 0) {
            throw new BusinessException(EmBussinessError.PARAMETER_VALIDATE_ERROR,"参数有误");
        }
        int effectiveRow = itemStockMapper.decreseStock(itemId,amount);
        if (effectiveRow > 0) {
            return true;
        }
        return false;
    }

    private ItemModel convertToItemModel(Item item, ItemStock itemStock) {
        ItemModel itemModel = new ItemModel();
        if (item == null ) {
            return null;
        }
        BeanUtils.copyProperties(item,itemModel);
        if (itemStock == null) {
            itemModel.setStock(0);
        }
        itemModel.setStock(itemStock.getStock());
        return itemModel;
    }

    private Item convertToItem(ItemModel itemModel) {
        if (itemModel == null) {
            return null;
        }
        Item item = new Item();
        BeanUtils.copyProperties(itemModel,item);
        return item;
    }
}
